﻿// Written by Tim Grant and Adam Bradford for CS3500, Nov. 2012

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Boggle
{
    /// <summary>
    /// 
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Passes the command-line arguments to ProcessArgs, which instantiates a BoggleServer.
        /// </summary>
        /// <param name="args">Command-line arguments.</param>
        /// <returns>Returns 0 if the program exits without errors; otherwise, returns 1;</returns>
        public static int Main(string[] args)
        {
            Console.WriteLine("Boggle Server");

            // non-zero value indicates an error occurred
            int exitCode;
            
            BoggleServer server = ProcessArgs(args, out exitCode);

            // if no error occurred when instantiating the server, wait until the user presses
            // ENTER to exit the program
            if (exitCode == 0)
            {
                try
                {
                    Console.WriteLine("Press ENTER to exit at any time..");
                    Console.ReadLine();                    
                }
                catch (Exception)
                {

                }
                finally
                {
                    server.Stop();
                }
            }

            return exitCode;
        }

        /// <summary>
        /// Validates the command-line arguments and instantiates a BoggleServer 
        /// based upon their contents.
        /// </summary>
        static BoggleServer ProcessArgs(string[] args, out int code)
        {
            BoggleServer server;
            
            try
            {
                //time = 60;
                //file = "..//..//..//dictionary.txt";
                //board = "TESEFHGOACAOYTIQ";

                int time;
                string file, board;

                // make sure there are the right number of arguments
                if (args.Length < 2 || args.Length > 3)
                {
                    throw new ArgumentException("Error: There must be 2 or 3 arguments.");
                }

                // make sure the first argument is an integer
                if (!Int32.TryParse(args[0], out time) | time < 0)
                {
                    throw new ArgumentException("Error: First argument must be a positive integer.");
                }

                // validate the file path
                file = args[1];
                if (!File.Exists(file))
                {
                    throw new FileNotFoundException("Error: Dictionary file not found.");
                }

                // instantiate the server
                if (args.Length == 2)
                {
                    server = new BoggleServer(time, file);
                }
                else
                {
                    // make sure the specified board is valid
                    if (Regex.IsMatch(args[2], "^[aA-zZ]{16}$"))
                    {
                        board = args[2];
                        server = new BoggleServer(time, file, board);
                    }
                    else
                    {
                        throw new ArgumentException("Error: Speficied board must be exactly 16 letters.");
                    }
                }
            }
            catch (Exception ex)
            {
                // print an error message if the arguments are invalid
                Console.Error.WriteLine(ex.Message);
                code = 1;
                return server = null;
            }

            code = 0;
            return server;
        }
    }
}
